﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DemoManageSys.Areas.Filters;
using Models.DTO;
using Microsoft.AspNetCore.Mvc;
using Models;
using IDAL;
using IBLL;
using DAL;

namespace DemoManageSys.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class CustomerInfoController : Controller
    {
        private ICustomerInfoBLL _customerInfoBLL;

        public CustomerInfoController(
            ICustomerInfoBLL customerInfoBLL
            )
        {
            _customerInfoBLL = customerInfoBLL;
        }

        #region 显示视图
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateView()
        {
            return View();
        }

        public IActionResult EditView()
        {
            return View();
        }
        #endregion

        #region 获取顾客表的方法 (GetCustomerInfo)
        [HttpGet]
        public IActionResult GetCustomerInfoH(int page, int limit, string customerName)
        {
            int count;
            //调用BLL的查询方法

            List<GetCustomerInfoDTO> getCustomerInfoDTOs = _customerInfoBLL.GetCustomerInfoH(page, limit, customerName, out count);

            ReturnResule result = new ReturnResule()
            {
                Code = 0,
                Msg = "获取成功",
                Data = getCustomerInfoDTOs,
                Count = count
            };

            return Json(result);
        }
        #endregion ()

        #region 添加的方法 (CreateCustomerInfo)
        [HttpPost]
        public IActionResult CreateCustomerInfo([FromForm] CustomerInfo cust)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            string msg;//定义返回消息
            resule.IsSuccess = _customerInfoBLL.CreateCustomerInfo(cust, out msg);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Code = 200;
                resule.Msg = msg;
            }
            else
            {
                resule.Msg = msg;

            }
            return Json(resule);
        }
        #endregion

        #region 删除的方法 (DeleteCustomerInfo)
        /// <summary>
        /// 删除顾客
        /// </summary>
        /// <param name="user">顾客实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteCustomerInfo(string id)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "删除顾客的id不能为空";
                return Json(resule);
            }

            resule.IsSuccess = _customerInfoBLL.DeleteCustomerInfo(id);//调用方法传值
            if (resule.IsSuccess)
            {
                resule.Msg = "删除顾客成功";
                resule.Code = 200;
            }
            else
            {
                resule.Msg = "删除顾客失败";
            }
            return Json(resule);
        }
        #endregion

        #region 批量软删除顾客的方法 (DeleteListCustomerInfo)
        /// <summary>
        /// 批量删除顾客
        /// </summary>
        /// <param name="ids">顾客实体</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteListCustomerInfo(List<string> ids)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();
            if (ids.Count == 0)
            {
                resule.Msg = "你没有返回任何顾客";
                return Json(resule);
            }
            bool isOK = _customerInfoBLL.DeleteListCustomerInfo(ids);//调用方法传值
            resule.Code = 200;
            resule.IsSuccess = isOK;
            resule.Msg = "删除顾客成功";
            return Json(resule);
        }
        #endregion

        #region 修改顾客的方法 (UpdateCustomerInfo)
        /// <summary>
        /// 修改顾客的方法
        /// </summary>
        /// <param name="cust"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCustomerInfo([FromForm] CustomerInfo cust)
        {
            //实例化对象
            ReturnResule resule = new ReturnResule();

            string msg;//定义返回消息
            resule.IsSuccess = _customerInfoBLL.UpdateCustomerInfo(cust, out msg);//调用方法传值

            resule.Msg = msg;
            if (resule.IsSuccess)
            {
                resule.Code = 200;
            }

            return Json(resule);
        }
        #endregion

        #region 根据ID获取下拉框 (GetCustomerInfoById)
        [HttpGet]
        public IActionResult GetCustomerInfoById(string id)
        {
            ReturnResule resule = new ReturnResule();

            if (string.IsNullOrWhiteSpace(id))
            {
                resule.Msg = "顾客ID为空";
                return Json(resule);
            }

            CustomerInfo cust = _customerInfoBLL.GetCustomerInfoById(id);
            if (cust == null)
            {
                resule.Msg = "未获取到顾客信息";
            }
            else
            {
                var selectOption = _customerInfoBLL.GetSelectOption();
                resule.Msg = "成功";
                resule.Code = 200;
                resule.Data = new
                {
                    cust = cust,//顾客信息
                    SelectOption = selectOption//下拉列表数据
                };
            }
            return Json(resule);
        }
        #endregion

        #region 获取下拉框的方法 (GetSelectOption)
        /// <summary>
        /// 获取下拉框的方法
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSelectOption()
        {
            ReturnResule resule = new ReturnResule();
            var data = _customerInfoBLL.GetSelectOption();

            if (data != null)
            {
                resule.Msg = "返回成功";
                resule.Data = data;
                resule.Code = 200;
            }
            return Json(resule);
        }
        #endregion
    }
}